package com.bidevalution.controller.api;

import java.util.Date;
import java.util.Map;

import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.bidevalution.business.service.BeMethodDefineService;
import com.bidevalution.controller.dto.BeMethodDefineDTO;
import com.bidevalution.dao.entity.BeMethodDefine;
import com.bidevalution.dao.extend.BeMethodDefineExt;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qtp.core.model.Paging;
import com.qtp.core.model.Response;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/beMethodDefine")
@Api(tags = "评标方法管理")
public class BeMethodDefineController extends BaseController {
	@Autowired
	private BeMethodDefineService beMethodDefineService;

	@ApiOperation(value = "分页查询评标方法", notes = "根据查询条件分页查询评标方法", response = BeMethodDefineExt.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "页数量", required = true, dataType = "Integer", paramType = "query") })
	@GetMapping("/getMethodDefinePage")
	public Response<Paging<BeMethodDefineExt>> selectList(@RequestParam("pageNum") int pageNum,
			@RequestParam("pageSize") int pageSize) {
		Page<BeMethodDefineExt> beMethodDefineExtPage = PageHelper.startPage(pageNum, pageSize);
		return Response.ok(
				new Paging<BeMethodDefineExt>(beMethodDefineExtPage.getTotal(), beMethodDefineService.selectForList()));
	}

	@ApiOperation(value = "查询评标方法详情", notes = "根据主键值查询评标方法详情", response = BeMethodDefine.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "seqId", value = "主键", required = true, dataType = "String", paramType = "query") })
	@GetMapping("/getMethodDefine")
	public Response<BeMethodDefine> selectById(@RequestParam("seqId") String seqId) {
		return Response.ok(beMethodDefineService.selectByPrimaryKey(seqId));
	}

	@PostMapping("/addMethodDefine")
	@ApiOperation(value = "新增评标方法", notes = "新增评标方法")
	public Response<String> add(BeMethodDefineDTO beMethodDefineDTO) {
		BeMethodDefine beMethodDefine = new BeMethodDefine();
		beMethodDefine.setArea(beMethodDefineDTO.getArea());
		beMethodDefine.setCode(beMethodDefineDTO.getCode());
		beMethodDefine.setCreateTime(new Date());
		beMethodDefine.setUpdateTime(new Date());
		beMethodDefine.setTemplateConfig(beMethodDefineDTO.getTemplateConfig());
		beMethodDefine.setName(beMethodDefineDTO.getName());
		beMethodDefine.setMemo(beMethodDefineDTO.getMemo());
		beMethodDefineService.insert(beMethodDefine);
		return Response.ok();
	}

	@ApiOperation(value = "删除评标方法", notes = "根据主键删除评标方法")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "seqId", value = "主键", required = true, dataType = "String", paramType = "query") })
	@PostMapping("/delMethodDefine")
	public Response<String> del(@RequestParam("seqId") String seqId) {
		beMethodDefineService.deleteById(seqId);
		return Response.ok();
	}

	@PostMapping("/updateMethodDefine")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "seqId", value = "主键", required = true, dataType = "String", paramType = "query") })
	@ApiOperation(value = "修改评标方法", notes = "修改评标方法")
	public Response<String> update(@RequestParam(value = "seqId", required = true) String seqId,
			BeMethodDefineDTO beMethodDefineDTO) {
		log.info(beMethodDefineDTO.getArea() + beMethodDefineDTO.getMemo());
		BeMethodDefine beMethodDefine = beMethodDefineService.selectByPrimaryKey(seqId);
		if (beMethodDefine != null) {
			beMethodDefine.setArea(beMethodDefineDTO.getArea());
			beMethodDefine.setCode(beMethodDefineDTO.getCode());
			beMethodDefine.setCreateTime(new Date());
			beMethodDefine.setUpdateTime(new Date());
			beMethodDefine.setTemplateConfig(beMethodDefineDTO.getTemplateConfig());
			beMethodDefine.setName(beMethodDefineDTO.getName());
			beMethodDefine.setMemo(beMethodDefineDTO.getMemo());
			beMethodDefineService.update(beMethodDefine);
			return Response.ok();
		} else {
			return Response.fail("记录不存在");
		}

	}
	@ApiOperation(value = "查询方法定义列表", notes = "查询方法定义列表")
	@PostMapping("/selectMethodDefineList")
	public Response<Map<String, Object>> selectMethodDefineList() {
		Map<String, Object> returnMap = Maps.newHashMap();
		for(BeMethodDefine beMethodDefine:beMethodDefineService.selectList()) {
			returnMap.put(beMethodDefine.getSeqId(), beMethodDefine.getName());
		}
		return Response.ok(returnMap);
	}
}
